package com.example.hospital.controller;

import com.example.hospital.pojo.AdminLoginParam;
import com.example.hospital.pojo.AdminUser;
import com.example.hospital.pojo.RespBean;
import com.example.hospital.pojo.Role;
import com.example.hospital.service.IAdminUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.List;

@Api(tags = "LoginController")
@RestController
public class LoginController {
    @Autowired
    private IAdminUserService adminUserService;

    @ApiOperation(value = "登录之后返回token")
    @PostMapping("/login")
    public RespBean login(@RequestBody AdminLoginParam adminLoginParam, HttpServletRequest request){
         return adminUserService.login(adminLoginParam.getUsername(),adminLoginParam.getPassword(),adminLoginParam.getCode(),request);
    }

    @ApiOperation(value = "获取当前登录用户的信息")
    @GetMapping("/admin/info")
    public AdminUser getAdminInfo(Principal principal){
        if(null==principal){
            return null;
        }
        String username = principal.getName();
        AdminUser adminUser = adminUserService.getAdminByUserName(username);
        adminUser.setPassword(null);
        adminUser.setRoles(adminUserService.getRoles(adminUser.getId()));
        return adminUser;
    }

    @ApiOperation(value = "退出登录")
    @PostMapping("/logout")
    public RespBean logout(){
        return RespBean.success("注释成功");
    }

}
